package com.cly.house.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cly.house.dao.DeptDao;
import com.cly.house.dao.impl.DeptDaoImpl;
import com.cly.house.entity.Dept;

/**
 * Servlet implementation class DeptServlet
 */
@WebServlet("/DeptServlet")
public class DeptServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       DeptDao deptDao = new DeptDaoImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeptServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
    
   
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
    	String type = request.getParameter("type");
		if(type!=null) {
			switch (type) {
			case "list":
				list(request,response);
				break;
			case "toAdd":
				toAdd(request,response);
				break;
			case "add":
				add(request,response);
				break;
			case "toUpdate":
				toUpdate(request,response);
				break;
			case "update":
				update(request,response);
				break;
			case "delete":
				delete(request,response);
				break;
			default:
				break;
			}
		}
	}
	
	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Dept> list = deptDao.listAll();
		request.setAttribute("deptList", list);
		request.getRequestDispatcher("WEB-INF/jsp/dept2/dept_list.jsp").forward(request, response);
	}
	protected void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
			request.getRequestDispatcher("WEB-INF/jsp/dept2/dept_add.jsp").forward(request, response);
		
		
		
	}
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String pname = request.getParameter("pname");
		String premark = request.getParameter("premark");
		Dept dept = new Dept();
		dept.setPname(pname);
		dept.setPremark(premark);
		dept.setPflag(0);
		int i = deptDao.save(dept);
		if(i!=0) {
			response.sendRedirect("DeptServlet?type=list");
		}
	}
	protected void toUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int pid = Integer.parseInt(request.getParameter("pid"));
		Dept deptId = deptDao.updateId(pid);
		request.setAttribute("dept", deptId);
		request.getRequestDispatcher("WEB-INF/jsp/dept/dept_update.jsp").forward(request, response);
		
	
	
	}
	protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int pid = Integer.parseInt(request.getParameter("pid"));
		String pname = request.getParameter("pname");
		String premark = request.getParameter("premark");
		Dept dept = new Dept();
		dept.setPid(pid);
		dept.setPflag(0);
		dept.setPname(pname);
		dept.setPremark(premark);
		int update = deptDao.update(dept);
		if(update>0) {
			response.sendRedirect("DeptServlet?type=list");
		}
	
	}
	protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int pid = Integer.parseInt(request.getParameter("pid"));
		Dept deptId = deptDao.updateId(pid);
		request.setAttribute("dept", deptId);
		boolean delete = deptDao.delete(pid);
		if(delete) {
			response.sendRedirect("DeptServlet?type=list");
		}
		
		
	
	
	}
}
